export function generateRandomString(n = 16) {
  const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
  // 创建字节数组来存储加密安全的随机值
  const randomBytes = new Uint8Array(n)
  window.crypto.getRandomValues(randomBytes) // 用随机字节填充数组

  let result = ''
  for (let i = 0; i < n; i++) {
    // 将随机字节映射到字符集的索引
    // 使用模运算 (%) 确保索引在 [0, chars.length) 范围内
    const randomIndex = randomBytes[i] % chars.length
    result += chars[randomIndex]
  }
  return result
}
